package com.thrsdk.ui.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import com.thrsdk.utils.PermissionHelper;
import com.thrsdk.utils.PermissionInterface;

public class DYFusionPerActivity extends Activity implements PermissionInterface {

    private PermissionHelper mPermissionHelper;
    protected ActivityInfo info = null;
    private ImageView kl_splash_iv;
    private int delay = 2000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        openFullScreenModel(this);
        super.onCreate(savedInstanceState);
        // 应用安装后，通过安装界面的打开按钮打开应用，每当应用从后台切换到前台，都会启动欢迎界面。如果是通过点击应用启动，则没有此问题。
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            Log.e("", "SplashActivity :onCreate");
            finish();
            return;
        }
        setContentView(resourceId("thf_activity_game_splash", "layout"));
        kl_splash_iv = findViewById(resourceId("thf_splash_iv", "id"));
        try {
            info = this.getPackageManager().getActivityInfo(this.getComponentName(), PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
//        initPermission();
        startMainActivity();

    }

    public void startMainActivity() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (info != null) {
                    String msg = info.metaData.getString("pro_main");
                    try {
                        final Class<?> mainClass = Class.forName(msg);
                        Intent intent = new Intent(DYFusionPerActivity.this, mainClass);
                        startActivity(intent);
                        finish();
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, delay);
    }

    private void initPermission() {
        //初始化并发起权限申请
        mPermissionHelper = new PermissionHelper(DYFusionPerActivity.this, this);
        mPermissionHelper.requestPermissions(this);
    }

    @Override
    public int getPermissionsRequestCode() {
        return 10230;
    }

    @Override
    public String[] getPermissions() {
        String thirdPer = info.metaData.getString("pro_per");
        if (thirdPer != null) {
            if (!"".equals(thirdPer)) {
                /**
                 * 如需申请更多的权限，请在new String[]中添加
                 */
                return new String[]{
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_PHONE_STATE,
                        thirdPer
                };
            }
        }
        return new String[]{
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE
        };
    }

    @Override
    public void requestPermissionsSuccess(Context context) {
        startMainActivity();
    }

    @Override
    public void requestPermissionsFail(Context context) {
        startMainActivity();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 10230) {
            startMainActivity();
        }
    }

    public int resourceId(String name, String type) {
        return getResources().getIdentifier(name, type, getPackageName());
    }

    @Override
    public void onBackPressed() {

    }

    public static void openFullScreenModel(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
            lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
            activity.getWindow().setAttributes(lp);

            View decorView = activity.getWindow().getDecorView();
            int systemUiVisibility = decorView.getSystemUiVisibility();

            int flags = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN;
            systemUiVisibility |= flags;
            activity.getWindow().getDecorView().setSystemUiVisibility(systemUiVisibility);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // 全屏显示，隐藏状态栏和导航栏，拉出状态栏和导航栏显示一会儿后消失。
            activity.getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }
}

